package sp.controller.exception;

import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.ExcessiveAttemptsException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import sp.util.constant.SysConstant;

/**
 * 全局异常处理
 * TODO
 * @description: 
 * @author 浮~沉
 * @date 2020年5月11日
 */
@ControllerAdvice
public class GlobalExceptionHandler {
	
	@ResponseBody
	@ExceptionHandler(value = {
		UnknownAccountException.class,
		IncorrectCredentialsException.class,
		LockedAccountException.class,
		ExcessiveAttemptsException.class,
		UnauthorizedException.class, 
		NoSuchElementException.class,
		AuthenticationException.class, RuntimeException.class, IllegalStateException.class})
	public Map<String, Object> handleUn(Exception e) {
		HashMap<String, Object> map = new HashMap<>();
		if (e.getMessage() == null) {
			map.put(SysConstant.ERROR, "系统异常,请及时和管理员取得联系");
			return map;
		}
		System.err.println("异常信息:" + e.getMessage());
		map.put(SysConstant.ERROR, e.getMessage());
		return map;
	}
}
